package com.chocho.producerserver.service;

import com.chocho.producerserver.bean.Module;
import com.chocho.producerserver.bean.User;
import com.chocho.producerserver.dao.ModuleDao;
import com.chocho.producerserver.dao.PermissionDao;
import com.chocho.producerserver.dao.UserDao;
import com.chocho.producerserver.vo.TokenInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * Created by chocho on 2022/4/16.
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //从数据库获取用户信息、权限信息。
        User user = userDao.selectByUsername(s);
        if(Objects.isNull(user)){
            throw new RuntimeException("用户名或者密码错误");
        }
        List<String> list = permissionDao.selectNameByUserID(user.getUserId());

        //将信息封装到UserDetails类的实现类中。
        return new TokenInfo(user, list);
    }
}
